const tools = {
  // 获取 cookie
  getCookie(key) {
    const cookies = document.cookie.split('; ')
       
        if (!key) {
          const result = {}
          for (let i = 0; i < cookies.length; i += 1) {
            const [name, value] = cookies[i].split('=')
            result[name] = value
          }
          return result
        }
        for (let i = 0; i < cookies.length; i += 1) {
          const [name, value] = cookies[i].split('=')
          if (name === key) {
            return value
          }
        }
        return null // 添加错误处理
  },
  // 设置cookie
  setCookie(key, value, expires, path) {
    let str = `${key}=${value}`
    if (expires) {
      const time = new Date()
      time.setTime(time.getTime() - 8 * 60 * 60 * 1000 + expires * 1000)
      str += `;expires=${time.toUTCString()}`
    }
    if (path) {
      str += `;path=${path}`
    }
    document.cookie = str
  },
}
export default tools
